package org.bot.socket.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.bot.client.driver.api.server.ServerReceiveClientData;
import org.springframework.stereotype.Component;

/**
 * @author 沧月
 * @create 0002 2023/10/2 15:46
 * @description <description class purpose>
 */
@Component
public class PushServerImpl implements ServerReceiveClientData {

    private int sendMaxLength = 1000;

    private JSONObject msgObj(String type, Object data) {
        JSONObject sendData = new JSONObject();
        sendData.fluentPut("msgType", type)
                .fluentPut("data", data);
        return sendData;
    }

    @Override
    public void clientLog(JSONObject log) {
        SocketClient.CLIENT.send(msgObj("log", log).toString());
    }

    @Override
    public void clientExecLog(JSONObject log) {
        if (log != null && log.getString("eventData") != null) {
            String eventData = log.getString("eventData");
            if (eventData.length() > sendMaxLength) {
                log.fluentPut("eventData", eventData.substring(0, sendMaxLength));
            }
        }
        SocketClient.CLIENT.send(msgObj("logInfo", log).toString());
    }

    @Override
    public void clientEvent(String clientId, JSONArray data) {
        SocketClient.CLIENT.send(msgObj("event", data).toString());
    }

    @Override
    public void clientStatus(String clientId, JSONObject data) {
        SocketClient.CLIENT.send(msgObj("status", data).toString());
    }
}
